var _api = require("../../utils/api");

function _slicedToArray(t, n) {
    return _arrayWithHoles(t) || _iterableToArrayLimit(t, n) || _nonIterableRest();
}

function _nonIterableRest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance");
}

function _iterableToArrayLimit(t, n) {
    var e = [], i = !0, r = !1, a = void 0;
    try {
        for (var o, s = t[Symbol.iterator](); !(i = (o = s.next()).done) && (e.push(o.value), 
        !n || e.length !== n); i = !0) ;
    } catch (t) {
        r = !0, a = t;
    } finally {
        try {
            i || null == s.return || s.return();
        } finally {
            if (r) throw a;
        }
    }
    return e;
}

function _arrayWithHoles(t) {
    if (Array.isArray(t)) return t;
}

Component({
    properties: {
        value: {
            type: String,
            value: ""
        },
        isShown: {
            type: Boolean,
            value: !1,
            observer: function(t) {
                t && this.initData();
            }
        },
        showReset: {
            type: Boolean,
            value: !1
        }
    },
    data: {
        reset: !1
    },
    methods: {
        initData: function() {
            var d = this;
            _api.json.getAllRegion().then(function(t) {
                var n = t.data, e = n.province_list, i = n.city_list, r = n.county_list, a = d.formatList(e);
                d.city_list = d.formatList(i), d.county_list = d.formatList(r);
                var o = d.data, s = o.value, u = o.showReset, l = {
                    provinces: a
                }, c = 0, f = 0;
                if (s) {
                    var v = _slicedToArray(s.split(" "), 2), y = v[0], h = v[1];
                    c = d.findIndex(a, y) || 0, d.provinceIndex = c, l.reset = u && !0, l.cities = d.findCounty(a[c]), 
                    f = d.findIndex(l.cities, h) || 0;
                }
                d.setData(l, function() {
                    d.onChange({
                        detail: {
                            value: [ c, f ]
                        }
                    });
                });
            });
        },
        isMunicipality: function(t) {
            return [ 11e4, 12e4, 31e4, 5e5 ].includes(+t);
        },
        formatList: function(n) {
            return Object.keys(n).map(function(t) {
                return {
                    key: t,
                    value: n[t]
                };
            });
        },
        findCounty: function(t) {
            var n = t.key, e = (t.value, n.slice(0, 2)), i = this.isMunicipality(n);
            return this[i ? "county_list" : "city_list"].filter(function(t) {
                return t.key.startsWith(e);
            });
        },
        findIndex: function(t, n) {
            return t.findIndex(function(t) {
                return t.value === n;
            });
        },
        onChange: function(t) {
            var n = _slicedToArray(t.detail.value, 2), e = n[0], i = n[1], r = this.data.provinces[e], a = this.findCounty(r), o = {};
            e !== this.provinceIndex && (i = 0, o.cities = a, this.provinceIndex = e), o.newVal = [ e, i ], 
            this.setData(o), this.value = "".concat(r.value, " ").concat(a[i].value);
        },
        onCancel: function() {
            this.setData({
                isShown: !1
            });
        },
        onReset: function() {
            this.triggerEvent("confirm", {
                value: ""
            }, {}), this.onCancel();
        },
        onConfirm: function() {
            this.triggerEvent("confirm", {
                value: this.value
            }, {}), this.onCancel();
        }
    }
});